//
// 作者：AKA_GGbond
// 时间：2025/7/17
// 邮箱：evain_wl@163.com
//

#include "W_RegistrationForm.h"
#include "../Compoment/W_InputBox.h"
#include "../DBFile/W_Sql.h"
#include <QIntValidator>
#include <QPropertyAnimation>
#include <QPainter>
#include <QGridLayout>

W_RegistrationForm::W_RegistrationForm(QWidget *parent)
    : QWidget(parent)
{
    this->resize(477, 620);

    username = new W_InputBox(":/Img/account.png", this);
    username->move(46, 130);
    username->setPlaceholderText("Username");
    username->setMaxLength(11);
//    username->setValidator(new QIntValidator(0, 9999999999, this));

    password = new W_InputBox(":/Img/password.png", this);
    password->move(46, 220);
    password->setPlaceholderText("Password");
    password->setEchoMode(QLineEdit::Password);
    password->setMaxLength(16);
    password->setValidator(new QRegularExpressionValidator(QRegularExpression("[a-zA-Z0-9]+$"), this));

    rootpsword = new W_InputBox(":/Img/password.png", this);
    rootpsword->move(46, 310);
    rootpsword->setPlaceholderText("RootPassword");
    rootpsword->setEchoMode(QLineEdit::Password);
    rootpsword->setMaxLength(16);
    rootpsword->setValidator(new QRegularExpressionValidator(QRegularExpression("[a-zA-Z0-9]+$"), this));

    login_button = new W_LoginButton(this);
    login_button->setCenter_text("注册");
    login_button->move(46, 400);

    this->animations();
    connect(login_button, &W_LoginButton::execute_animation_signal, this, &W_RegistrationForm::execute_animation);
    connect(login_button,&W_LoginButton::clicked,this,&W_RegistrationForm::onRegistraClicked);
}

W_RegistrationForm::~W_RegistrationForm()
{

}

void W_RegistrationForm::paintEvent(QPaintEvent *event)
{
    QPainter painter(this);
    painter.setRenderHint(QPainter::Antialiasing);
    painter.setRenderHint(QPainter::TextAntialiasing);
    painter.setViewport(0, 0, 477, 620);
    painter.setWindow(0, 0, 477, 620);

    this->crop_corner();
    this->draw_text();
}

void W_RegistrationForm::execute_animation(W_LoginButton::AnimationState State)
{
    if (State == W_LoginButton::Execute)
    {
        animation->setDirection(QAbstractAnimation::Forward);
        animation->start();
    }
    else if (State == W_LoginButton::Restore)
    {
        animation->setDirection(QAbstractAnimation::Backward);
        animation->start();
    }
}

void W_RegistrationForm::animations()
{
    animation = new QPropertyAnimation(this->login_button, "geometry");
    animation->setDuration(250);
    animation->setStartValue(this->login_button->geometry());
    animation->setEndValue(QRect(this->login_button->pos().x() + zoom_rate, this->login_button->pos().y() + zoom_rate / 2, login_button->width() - zoom_rate * 2, login_button->height() - zoom_rate));
    animation->setEasingCurve(QEasingCurve::Linear);
}

void W_RegistrationForm::crop_corner()
{
    QPainter painter(this);
    painter.setRenderHint(QPainter::Antialiasing);
    painter.setPen(Qt::NoPen);
    QBrush Brush(QColor(255, 255, 255, 255));
    painter.setBrush(Brush);
    painter.drawRect(0, 0, width(), height());
}

void W_RegistrationForm::draw_text()
{
    QPainter painter(this);
    painter.setRenderHint(QPainter::Antialiasing);
    painter.setRenderHint(QPainter::TextAntialiasing);

    QRect rect1(0, 0, 0, 0);
    QFont font1;
    font1.setPointSize(30);
    font1.setBold(true);
    font1.setWordSpacing(1);
    painter.setFont(font1);
    QColor semiTransparent(0, 0, 0, 255);
    painter.setPen(semiTransparent);

    QRect actualRect = painter.boundingRect(rect1, Qt::AlignCenter, "注册");
    rect1.setHeight(actualRect.height());
    rect1.setWidth(actualRect.width());
    rect1.moveCenter(QPoint(width() / 2, height() / 8));
    painter.drawText(rect1, "注册");
}

void W_RegistrationForm::onRegistraClicked()
{
    //注册
    QString username = this->username->text();
    QString password = this->password->text();
    QString rootPasswd = this->rootpsword->text();

    //首先查询新用户是否已经存在
    QString search_sql =  QString("SELECT CASE WHEN EXISTS (SELECT 1 FROM UserInfoModel WHERE userName = '%1') THEN 1 ELSE 0 END AS user_exists")
            .arg(username).arg(password);
    bool ret = W_SqlIns->Search(search_sql);
    if(ret) {
        //表示已经存在
        return;
    }

    //比对root密码，成功即可注册
    search_sql =  QString("SELECT CASE WHEN EXISTS (SELECT 1 FROM UserInfoModel WHERE userName = '%1') THEN 1 ELSE 0 END AS user_exists")
                     .arg("超级用户").arg(rootPasswd);
    ret = W_SqlIns->Search(search_sql);
    if(!ret) {
        //注册失败
        return;
    }

    QString insert_sql = QString("insert into UserInfoModel(userName,userPasswd) values ('%1','%2');")
                             .arg(username).arg(password);
    ret = W_SqlIns->executSQL(insert_sql);
    if(!ret) {
        //注册失败
        return;
    }

    //注册成功
}
